package com.beiming.libra.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.beiming.libra.common.utils.HttpClientUtils;
import com.beiming.libra.service.VerifyService;

import lombok.extern.slf4j.Slf4j;

/**
 * 实名认证
 * 
 * @author weibo
 *
 */
@Service
@Slf4j
public class VerifyServiceImpl implements VerifyService {

  @Value("${evaluate.context}")
  private String context;

  @Value("${evaluate.userName}")
  private String userName;

  @Value("${evaluate.password}")
  private String password;

  @Override
  public boolean identityAuthentication(String name, String idcard) {
    JSONObject loginResult = evaluateLogin(context);
    if ("200".equals(loginResult.getString("code"))) {
      String token = loginResult.getJSONObject("result").getString("token");
      JSONObject user = new JSONObject();
      user.put("name", name);
      user.put("idcard", idcard);
      String resultUser = HttpClientUtils.sendHttpPost(context + "/verified/user", user.toJSONString(), token);
      JSONObject resultDate = JSONObject.parseObject(resultUser);
      if ("200".equals(resultDate.getString("code"))) {
        return resultDate.getBooleanValue("result");
      }
    }
    return false;
  }

  private JSONObject evaluateLogin(String contextPath) {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("userName", userName);
    jsonObject.put("password", password);
    String loginStr = HttpClientUtils.sendHttpPost(contextPath + "/login/signIn", jsonObject.toString());
    return JSONObject.parseObject(loginStr);
  }
}
